<?php
/**
 * Framework_Meta_Common
 *
 * @author      Ian Szewczyk <roamzero@gmail.com>
 * @copyright   (c) 2007 Ian Szewczyk. All rights reserved.
 * @license     http://www.opensource.org/licenses/bsd-license.php
 * @package Framework
 * @subpackage Meta
 * @filesource
 */

include "File/Find.php";

/**
 * Framework_Meta_Common
 *
 *
 * @author Ian Szewczyk <roamzero@gmail.com>
 * @package Framework
 * @subpackage Meta
 */

class Framework_Meta_Common extends Framework_Object
{
	/**
     * $folder
     *
     * @access      public
     * @var         string		$folder      Folder to scan
     */
	public $folder;

	/**
     * $tableName
     *
     * @access      public
     * @var         string		$tableName     Name of the table to load into
     */
	public $tableName;

	/**
     * $table
     *
     * @access      private
     * @var         object		$table     Instance of table object
     */
	private $table;

	/**
     * $columns
     *
     * @access      private
     * @var         array		$columns     Array of column names
     */
	private $columns;

	/**
     * $cache
     *
     * @access      private
     * @var         object		$cache    Instance of Cache_Lite object
     */
	private $cache;

	/**
	 * __construct
	 *
	 * @access 		public
	 * @return 		void
	 */
	public function __construct()
	{
		$this->table = Framework_DB::tableFactory($this->tableName);

		if (PEAR::isError($this->table)) {
			throw new Framework_Exception('Invalid Table Loaded');
		} else {
			$this->columns = array_keys($this->table->col);
		}

		$this->cache = Framework_Cache::factory();

		if (PEAR::isError($this->cache)) {
			throw new Framework_Exception('Cache Could Not be loaded');
		}

		$result = $this->scan();

		if (PEAR::isError($result)) {
			throw new Framework_Exception($result->getMessage());
		}

	}

	/**
	 * getTable
	 *
	 * @access 		public
	 * @return 		object			Table instance
	 */
	public function getTable()
	{
		return $this->table;
	}

	/**
	 * scan
	 *
	 * @access 		private
	 * @return 		mixed			PEAR::error on failure, void
	 */
	private function scan()
	{
		//		$data = $this->cache->get($this->tableName,'Meta');
		//		if (PEAR::isError($data)) {
		//			return $data;
		//		} else {
		//
		//			if(!is_array($data)) {
		//				$data = array();
		//			}
			
		// first scan the framework base path
		$path = FRAMEWORK_BASE_PATH.'/'.$this->folder;
		$result = $this->loadMeta($path);
		if (PEAR::isError($result))
		return $result;
		// then scan the site path
		$path = Framework::$site->getPath().'/Framework/'.$this->folder;
		$result = $this->loadMeta($path);
		if (PEAR::isError($result))
		return $result;

		//}

	}

	/**
	 * loadMeta
	 *
	 * @access 		private
	 * @param		string			$path		Path to scan for meta.xml files
	 * @return 		mixed			PEAR::error on failure, void
	 */
	private function loadMeta($path)
	{

		$items = &File_Find::search('!meta\.xml$!', $path, 'perl' );
		if (count($items)) {
			foreach($items as $item) {
				
				$key = md5($item.filemtime($item));
				if (!$this->cache->get($key,'Meta')) {
					$meta = simplexml_load_file($item);
					if (!$meta instanceof SimpleXmlElement) {
						continue;
					}
						
						
					$children = $meta->children();
					
					$where = $children[0]->getName().' = '.'"'.(string)$children[0].'"';
					$sql = array(
						'sqlKey' => array(
							'select' => '*',
							
						),
						'filter' => $where
					);
						
						
					//if (!$this->table->selectCount($sql)) {
					$row = $this->table->getBlankRow();
					foreach($children as $name => $node){
						if (in_array($name, $this->columns)) {
							$row[$name] = (string) $node;
						}
					}
					
					if ($this->table->selectCount($sql) == 0) {
						
						$result = $this->table->insert($row);
					} else {
						
						$result = $this->table->update($row, $where);
					}

					if (PEAR::isError($result)) {
	
						return $result;
					}


			
						
					// Add to cache
					$this->cache->save($key,$key,'Meta');
						
				}
			}
		}

	}

	 /**
     * __call
     *
     * Routes function calls to the internal table object
     *
     * @access      public
     * @param       string      $function		Name of function
     * @param       array       $args		Array of arguments
     * @return      mixed
     */
	public function __call($function, $args)
	{

		return call_user_func_array(array(&$this->table,$function),$args);


	}






}

?>